package com.zhoug.android.common.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.view.Gravity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;


/**
 * 画图
 *
 * @Author 35574
 * @Date 2020/4/26
 * @Description
 */
public class CanvasUtils {
    private static final String TAG = ">>>CanvasUtils";



    /**
     * 在图片上画文字:水平方向
     *
     * @param bitmap    图片
     * @param text      需要画的文字
     * @param textColor 文字颜色
     * @param textSize  文字大小
     * @param gravity   位置:支持{@link Gravity#BOTTOM,Gravity#TOP}
     * @return
     */
    public static Bitmap drawTextOnBitmap(Context context,Bitmap bitmap, String text, int textColor, int textSize, int gravity) {
        if (bitmap == null) return null;

        TextPaint paint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.FILL);
        paint.setAntiAlias(true);
        paint.setColor(textColor);
        paint.setTextSize(ResourceUtils.spTopx(context,textSize));
        paint.setDither(true); // 获取跟清晰的图像采样
        paint.setFilterBitmap(true);// 过滤一些

        Bitmap.Config bitmapConfig = bitmap.getConfig();
        if (bitmapConfig == null) {
            bitmapConfig = Bitmap.Config.RGB_565;
        }
        bitmap = bitmap.copy(bitmapConfig, true);
        Canvas canvas = new Canvas(bitmap);
        int marging = 20;
        if (gravity == Gravity.TOP) {
            canvas.drawText(text, marging, marging, paint);
        } else {
            //文本的行宽
            int maxW = bitmap.getWidth() - marging * 2;
            //StaticLayout自动换行
            StaticLayout staticLayout = new StaticLayout(text, paint, maxW, Layout.Alignment.ALIGN_NORMAL, 1.0f, 10, false);
            //文本的总高
            int height = staticLayout.getHeight();
            //移动
            canvas.translate(marging, bitmap.getHeight() - height - marging);
            staticLayout.draw(canvas);

//            canvas.drawText(text, marging, y, paint);
        }
        return bitmap;
    }

    /**
     * 把画布剪切为圆型画布
     *
     * @param canvas
     */
    public static @NonNull Path getRoundPath(Canvas canvas) {
        Path path = new Path();
        int width = canvas.getWidth();
        int height = canvas.getHeight();
        int radius = Math.min(width / 2, height / 2);
        path.addCircle(width >> 1, height >> 1, radius, Path.Direction.CW);
        return path;
    }

    /**
     * 把画布剪切为圆角画布
     * @deprecated 废弃canvas.clipPath会有严重的锯齿效果,无法消除.
     * 使用canvas.drawPath(path, paint)代替,Paint设置抗锯齿.然后用Paint的PorterDuff.Mode实现所需效果
     * @param canvas  Canvas
     * @param corners 角度
     * @param lt      左上角
     * @param rt      右上角
     * @param rb      右下角
     * @param lb      左下角
     * 参考{@link #getCornersPath(Canvas, int, boolean, boolean, boolean, boolean)}
     */
    @Deprecated
    public static void clipCornersPath(Canvas canvas, int corners, boolean lt, boolean rt, boolean rb, boolean lb) {
        //不需要剪切
        if (corners <= 0 || !(lt || lb || rt || rb)) {
            return;
        }
        int width = canvas.getWidth();
        int height = canvas.getHeight();
        //最大角度
        int maxCorners = Math.min(width, height) / 2;
        if (corners > maxCorners) {
            corners = maxCorners;
        }
        Path path = new Path();
        //左上角
        path.moveTo(0, corners);
        if (lt) {
            path.quadTo(0, 0, corners, 0);
        } else {
            path.lineTo(0, 0);
        }

        path.lineTo(width - corners, 0);
        //右上角
        if (rt) {
            path.quadTo(width, 0, width, corners);
        } else {
            path.lineTo(width, 0);
        }
        path.lineTo(width, height - corners);
        //右下角
        if (rb) {
            path.quadTo(width, height, width - corners, height);
        } else {
            path.lineTo(width, height);
        }
        path.lineTo(corners, height);
        //左下角
        if (lb) {
            path.quadTo(0, height, 0, height - corners);
        } else {
            path.lineTo(0, height);
        }
        path.lineTo(0, corners);
        canvas.clipPath(path);
    }

    /**
     * 获取到画布上的圆角路径
     * @param canvas  Canvas
     * @param corners 角度
     * @param lt      左上角
     * @param rt      右上角
     * @param rb      右下角
     * @param lb      左下角
     */
    public static @Nullable Path getCornersPath(Canvas canvas, int corners, boolean lt, boolean rt, boolean rb, boolean lb) {
        //不需要剪切
        if (corners <= 0 || !(lt || lb || rt || rb)) {
            return null;
        }
        int width = canvas.getWidth();
        int height = canvas.getHeight();
        //最大角度
        int maxCorners = Math.min(width, height) / 2;
        if (corners > maxCorners) {
            corners = maxCorners;
        }
        Path path = new Path();
        //左上角
        path.moveTo(0, corners);
        if (lt) {
            path.quadTo(0, 0, corners, 0);
        } else {
            path.lineTo(0, 0);
        }

        path.lineTo(width - corners, 0);
        //右上角
        if (rt) {
            path.quadTo(width, 0, width, corners);
        } else {
            path.lineTo(width, 0);
        }
        path.lineTo(width, height - corners);
        //右下角
        if (rb) {
            path.quadTo(width, height, width - corners, height);
        } else {
            path.lineTo(width, height);
        }
        path.lineTo(corners, height);
        //左下角
        if (lb) {
            path.quadTo(0, height, 0, height - corners);
        } else {
            path.lineTo(0, height);
        }
        path.lineTo(0, corners);
        return path;
    }

    /**
     * 获取到画布上的圆角路径
     * @param width  width
     * @param height  height
     * @param corners 角度
     * @param lt      左上角
     * @param rt      右上角
     * @param rb      右下角
     * @param lb      左下角
     */
    public static @Nullable Path getCornersPath(int  width,int height, int corners, boolean lt, boolean rt, boolean rb, boolean lb) {
        //不需要剪切
        if (corners <= 0 || !(lt || lb || rt || rb)) {
            return null;
        }
        if(width<=0 || height<=0){
            return null;
        }

        //最大角度
        int maxCorners = Math.min(width, height) / 2;
        if (corners > maxCorners) {
            corners = maxCorners;
        }
        Path path = new Path();
        //左上角
        path.moveTo(0, corners);
        if (lt) {
            path.quadTo(0, 0, corners, 0);
        } else {
            path.lineTo(0, 0);
        }

        path.lineTo(width - corners, 0);
        //右上角
        if (rt) {
            path.quadTo(width, 0, width, corners);
        } else {
            path.lineTo(width, 0);
        }
        path.lineTo(width, height - corners);
        //右下角
        if (rb) {
            path.quadTo(width, height, width - corners, height);
        } else {
            path.lineTo(width, height);
        }
        path.lineTo(corners, height);
        //左下角
        if (lb) {
            path.quadTo(0, height, 0, height - corners);
        } else {
            path.lineTo(0, height);
        }
        path.lineTo(0, corners);
        return path;
    }

    /**
     * 文字居中,计算文字baseLIne
     * @param centerY 中线 :(top+bottom)/2
     * @param fontMetrics
     * @return
     */
    public static float getBaseLine(float centerY,Paint.FontMetrics fontMetrics){
        return  centerY-(fontMetrics.descent+fontMetrics.ascent)/2;
    }

    /**
     * 文字居中,计算文字baseLIne
     * @param centerY 中线 :(top+bottom)/2
     * @param paint
     * @return
     */
    public static float getBaseLine(float centerY,Paint paint){
        Paint.FontMetrics fontMetrics = paint.getFontMetrics();
        return  centerY-(fontMetrics.descent+fontMetrics.ascent)/2;
    }



}
